package org.xncj.Controller;

import org.apache.commons.lang3.mutable.Mutable;
import org.constant.MessageConstant;
import org.entity.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.utils.POIUtils;
import org.vo.OrderSettingVo;
import org.xncj.OrderSetting;
import org.xncj.OrderSettingService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingControll {
    @Resource
    OrderSettingService orderSettingService;
    @PostMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            orderSettingService.importPIO(list);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }
    @GetMapping("/findAllOdersetting")
    public Result findAllOdersetting() {
        try {
            List<OrderSetting> list = orderSettingService.findAllOdersetting();
            List<OrderSettingVo> ordersettingvos = new ArrayList<>();
            for (OrderSetting orderSetting : list) {
                OrderSettingVo vo = new OrderSettingVo();
                vo.setData(orderSetting.getOrderDate().getDate());
                vo.setNumber(orderSetting.getNumber());
                vo.setReservations(orderSetting.getReservations());
                ordersettingvos.add(vo);
            }
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, ordersettingvos);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS);
        }

    }
    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editNumberByDate(orderSetting);
            return  new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);

        }
    }
}
